package de.koller.worlders.gameplay.gui;

import de.koller.oceanFramework.glyph.GlyphScreenGL;
import de.koller.oceanFramework.tree.Child;
import de.koller.worlders.WSea;
import de.koller.worlders.engine.WInputGL;
import de.koller.worlders.gameplay.resource.FontValue;
import de.koller.worlders.render.IGLRender;

public class MouseHold extends WSea implements IGLRender {
	
	public static final int TEX_SIZE = 32;
	
	private int offsetX = 0, offsetY = 0;
	private float scaleX = 1, scaleY = 1;
	private int absX = 0, absY = 0;
	private boolean isAbsolute = false;
	private final float w = TEX_SIZE / 24f;
	
	@Child private final GlyphScreenGL gscreen = new GlyphScreenGL( TEX_SIZE, TEX_SIZE,
			new FontValue(), "de/koller/worlders/gameplay/resource/font.png" );

	@Override
	protected void init() {
		super.init();
		
		gscreen.setName( "MouseGLScreen" );
		
		wroot.rendering.addWorldElement( this );
	}

	@Override
	protected void release() {
		super.release();
		
		wroot.rendering.addWorldElement( this );
	}
	
	@Override
	public void renderGL( int elapsedTime ) {
		WInputGL i = wroot.getInput();
		int x = isAbsolute ? absX : i.getMouseX() - offsetX + i.getMouseDX();
		int y = isAbsolute ? absY : i.getMouseY() - offsetY + i.getMouseDY();

		gscreen.updateBuffer();
		gscreen.getTexture().draw2D( x, y, w, w, 15 );
		
		isAbsolute = false;
		scaleX = 1;
		scaleY = 1;
	}

	public GlyphScreenGL getVisual() {
		return gscreen;
	}
	
	public void setOffset( int offsetX, int offsetY ) {
		this.offsetX = offsetX;
		this.offsetY = offsetY;
	}
	
	public void setWorldPos( float x, float y ) {
		absX = Math.round( x );
		absY = Math.round( y );
		isAbsolute = true;
	}
	
	public void setScale( float sx, float sy ) {
		scaleX = sx;
		scaleY = sy;
	}
	
}
